package com.cyj.dream.datasource.config;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;
import javax.sql.DataSource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * @Description: druid数据源配置
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.datasource.config
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-27 09:50
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
/*
@Slf4j
@Configuration
public class DruidConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druid() {
        return new DruidDataSource();
    }

    */
/**
     * 配置druid管理页面的访问控制
     * 访问网址: http://127.0.0.1:8080/druid
     *
     * @return
     *//*

    @Bean
    public ServletRegistrationBean<Servlet> druidServlet() {
        log.info("init Druid Servlet Configuration");
        ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>();
        // 配置一个拦截器
        servletRegistrationBean.setServlet(new StatViewServlet());
        // 指定拦截器只拦截druid管理页面的请求
        servletRegistrationBean.addUrlMappings("/druid/*");
        Map<String, String> initParam = new HashMap<>(16);
        // 登录druid管理页面的用户名
        initParam.put("loginUsername", "admin");
        // 登录druid管理页面的密码
        initParam.put("loginPassword", "123456");
        // 是否允许重置druid的统计信息
        initParam.put("resetEnable", "true");
        // ip白名单，如果没有设置或为空，则表示允许所有访问
        initParam.put("allow", "");
        // 启用
        initParam.put("enabled", "true");
        servletRegistrationBean.setInitParameters(initParam);
        return servletRegistrationBean;
    }

    */
/**
     * 配置StatFilter
     *
     * @return
     *//*

    @Bean
    public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
*/
